﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace QXBook.Service
{
    public class ServiceFactory : ServiceBase
    {
        public static readonly object m_olocker = new object();

        public static T CreateInstance<T>()
        {
            Type type = typeof(T);

            if (System.Web.HttpContext.Current == null) return (T)Activator.CreateInstance(type);

            IDictionary dict = System.Web.HttpContext.Current.Items;
            if (!dict.Contains(type))
            {
                lock (m_olocker)
                {
                    if (!dict.Contains(type))
                    {
                        T result = (T)Activator.CreateInstance(type);
                        dict[type] = result;

                        return result;
                    }
                }
            }

            return (T)dict[type];
        }
    }
}
